package servlet;

import java.io.*;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.*;
import bll.*;
import model.*;
import dal.*;

/**
 * Servlet implementation class CompanyServlet
 */
@WebServlet(urlPatterns = { "/AgendaItem", "/agendaItem", "/AgendaItemServlet", "/agendaItemServlet" }, initParams = { @WebInitParam(name = "mode", value = "") })
public class AgendaItemServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public AgendaItemServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("new")) {

			if (RightUtil.<AgendaItemModel> checkInsertRight()) {

				// TODO : MORE CODE HERE

				getServletContext().getRequestDispatcher("/agendaItemNew.jsp").forward(request, response);
			} else {
				// TODO no right
			}
		} else if (mode.equalsIgnoreCase("edit")) {

			AgendaItemModel query = new AgendaItemModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<AgendaItemModel, AgendaItemModelExt> dal = new GenericDal<AgendaItemModel, AgendaItemModelExt>(new AgendaItemModel());

			AgendaItemModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {

				// TODO : MORE CODE HERE

				request.setAttribute("agendaItem", obj);
				getServletContext().getRequestDispatcher("/agendaItemEdit.jsp").forward(request, response);

			} else {
				// TODO : data not found
			}
		} else if (mode.equalsIgnoreCase("view")) {

			AgendaItemModel query = new AgendaItemModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<AgendaItemModel, AgendaItemModelExt> dal = new GenericDal<AgendaItemModel, AgendaItemModelExt>(new AgendaItemModel());

			AgendaItemModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {
				// TODO : LOAD MASTER DATA HERE
				// It's specific code that cannot be generated

				request.setAttribute("agendaItem", obj);
				getServletContext().getRequestDispatcher("/agendaItemView.jsp").forward(request, response);

			} else {
				// TODO : data not found
			}
		} else if (mode.equalsIgnoreCase("quickInsert")) {
			AgendaItemModel obj = new AgendaItemModel();

			// TODO set name here

			GenericDal<AgendaItemModel, AgendaItemModelExt> dal = new GenericDal<AgendaItemModel, AgendaItemModelExt>(new AgendaItemModel());

			int res = 0;
			try {
				res = dal.insert(obj);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// TODO : return value

		} else if (mode.equalsIgnoreCase("delete")) {

			AgendaItemModel query = new AgendaItemModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<AgendaItemModel, AgendaItemModelExt> dal = new GenericDal<AgendaItemModel, AgendaItemModelExt>(new AgendaItemModel());
			AgendaItemModel objToBeDeleted = null;
			int res = Integer.MIN_VALUE;
			try {
				objToBeDeleted = dal.select(query);
				res = dal.delete(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (res == 0) {
				// TODO success
				if (objToBeDeleted != null) {
					response.sendRedirect("agenda.jsp?id=" + objToBeDeleted.getIdAgenda());
				}
			} else {
				// TODO fail
				//Redirect to "Error page"
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("insert")) {

			AgendaItemModel obj = new AgendaItemModel();

			String id_agenda = request.getParameter("id_agenda");
			if (id_agenda != null && !id_agenda.isEmpty()) {
				obj.setIdAgenda(Integer.parseInt(id_agenda));
			}

			// String id_parent = request.getParameter("id_parent");
			// if (id_parent != null && !id_parent.isEmpty()) {
			// obj.setIdParent(Integer.parseInt(id_parent));
			// }

			String name = request.getParameter("name");
			obj.setName(name);

			String description = request.getParameter("description");
			obj.setDescription(description);

			String remark = request.getParameter("remark");
			obj.setRemark(remark);

			// String is_group = request.getParameter("is_group");
			// obj.setIsGroup(is_group != null);
			//
			// String id_prev_item = request.getParameter("id_prev_item");
			// if (!obj.getIsGroup() && id_prev_item != null &&
			// !id_prev_item.isEmpty()) {
			// obj.setIdPrevItem(Integer.parseInt(id_prev_item));
			// }

			String start_time_date = request.getParameter("start_time_date");
			// String start_time_time = request.getParameter("start_time_time");
			if (start_time_date != null && !start_time_date.isEmpty()) {// &&
																		// start_time_time
																		// !=
																		// null
																		// &&
																		// !start_time_time.isEmpty())
																		// {
				obj.setStartTime(DateTimeUtil.parseDateTime(start_time_date));
				// } else {
				// obj.setStartTime(DateTimeUtil.parseDate(start_time_date));
			}

			String duration = request.getParameter("duration");
			if (duration != null && !duration.isEmpty()) {
				obj.setDuration(Integer.parseInt(duration));
			}

			String id_employee = request.getParameter("id_employee");
			if (!obj.getIsGroup() && id_employee != null && !id_employee.isEmpty()) {
				obj.setIdEmployee(Integer.parseInt(id_employee));
			}

			String id_company = request.getParameter("id_company");
			if (!obj.getIsGroup() && id_company != null && !id_company.isEmpty()) {
				obj.setIdCompany(Integer.parseInt(id_company));
			}

			String sound = request.getParameter("sound");
			obj.setSound(obj.getIsGroup() ? "" : sound);

			String light = request.getParameter("light");
			obj.setLight(obj.getIsGroup() ? "" : light);

			String left_screen = request.getParameter("left_screen");
			obj.setLeftScreen(obj.getIsGroup() ? "" : left_screen);

			String right_screen = request.getParameter("right_screen");
			obj.setRightScreen(obj.getIsGroup() ? "" : right_screen);

			String center_screen = request.getParameter("center_screen");
			obj.setCenterScreen(obj.getIsGroup() ? "" : center_screen);

			// String code = request.getParameter("code");
			// obj.setCode(obj.getIsGroup() ? "" : code);

			// String id_type = request.getParameter("id_type");
			// if (!obj.getIsGroup() && id_type != null && !id_type.isEmpty()) {
			// obj.setIdType(Integer.parseInt(id_type));
			// }

			String status = request.getParameter("status");
			if (!obj.getIsGroup() && status != null && !status.isEmpty()) {
				obj.setStatus(Integer.parseInt(status));
			}

			// String notification = request.getParameter("notification");
			// if (notification != null && !notification.isEmpty()) {
			// obj.setNotification(Integer.parseInt(notification));
			// }

			obj.setCreator(SessionUtil.getCurrentUserID());

			if (CommonBll.checkInputs(obj)) {
				GenericDal<AgendaItemModel, AgendaItemModelExt> dal = new GenericDal<AgendaItemModel, AgendaItemModelExt>(new AgendaItemModel());

				int res = 0;
				try {
					res = dal.insert(obj);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (res > 0) {
					// TODO : success, redirect to somewhere
					String url = "agenda.jsp?id=" + obj.getIdAgenda();
					response.sendRedirect(url);
					//getServletContext().getRequestDispatcher(url).forward(request, response);
				} else {
					// TODO : processing error here
				}
			} else {

				// invalid input

				request.setAttribute("agendaItem", obj);
				getServletContext().getRequestDispatcher("/agendaItemInput.jsp").forward(request, response);

			}
		} else if (mode.equalsIgnoreCase("update")) {

			AgendaItemModel query = new AgendaItemModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<AgendaItemModel, AgendaItemModelExt> dal = new GenericDal<AgendaItemModel, AgendaItemModelExt>(new AgendaItemModel());

			AgendaItemModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {

				String id_agenda = request.getParameter("id_agenda");
				if (id_agenda != null && !id_agenda.isEmpty()) {
					obj.setIdAgenda(Integer.parseInt(id_agenda));
				}

				String id_parent = request.getParameter("id_parent");
				if (id_parent != null && !id_parent.isEmpty()) {
					obj.setIdParent(Integer.parseInt(id_parent));
				}

				String name = request.getParameter("name");
				obj.setName(name);

				String description = request.getParameter("description");
				obj.setDescription(description);

				String remark = request.getParameter("remark");
				obj.setRemark(remark);

				// String is_group = request.getParameter("is_group");
				// obj.setIsGroup(is_group != null);
				//
				// String id_prev_item = request.getParameter("id_prev_item");
				// if (!obj.getIsGroup() && id_prev_item != null &&
				// !id_prev_item.isEmpty()) {
				// obj.setIdPrevItem(Integer.parseInt(id_prev_item));
				// }

				String start_time_date = request.getParameter("start_time_date");
				// String start_time_time =
				// request.getParameter("start_time_time");

				if (start_time_date != null && !start_time_date.isEmpty()) {// &&
																			// start_time_time
																			// !=
																			// null
																			// &&
																			// !start_time_time.isEmpty())
																			// {
					obj.setStartTime(DateTimeUtil.parseDateTime(start_time_date));
					// } else {
					// obj.setStartTime(DateTimeUtil.parseDate(start_time_date));
				}

				String duration = request.getParameter("duration");
				if (duration != null && !duration.isEmpty()) {
					obj.setDuration(Integer.parseInt(duration));
				}

				String id_employee = request.getParameter("id_employee");
				if (!obj.getIsGroup() && id_employee != null && !id_employee.isEmpty()) {
					obj.setIdEmployee(Integer.parseInt(id_employee));
				}

				String id_company = request.getParameter("id_company");
				if (!obj.getIsGroup() && id_company != null && !id_company.isEmpty()) {
					obj.setIdCompany(Integer.parseInt(id_company));
				}

				String sound = request.getParameter("sound");
				obj.setSound(obj.getIsGroup() ? "" : sound);

				String light = request.getParameter("light");
				obj.setLight(light);

				String left_screen = request.getParameter("left_screen");
				obj.setLeftScreen(obj.getIsGroup() ? "" : left_screen);

				String right_screen = request.getParameter("right_screen");
				obj.setRightScreen(obj.getIsGroup() ? "" : right_screen);

				String center_screen = request.getParameter("center_screen");
				obj.setCenterScreen(obj.getIsGroup() ? "" : center_screen);

				// String code = request.getParameter("code");
				// obj.setCode(obj.getIsGroup() ? "" : code);

				// String id_type = request.getParameter("id_type");
				// if (!obj.getIsGroup() && id_type != null &&
				// !id_type.isEmpty()) {
				// obj.setIdType(Integer.parseInt(id_type));
				// }

				String status = request.getParameter("status");
				if (!obj.getIsGroup() && status != null && !status.isEmpty()) {
					obj.setStatus(Integer.parseInt(status));
				}

				// String notification = request.getParameter("notification");
				// if (notification != null && !notification.isEmpty()) {
				// obj.setNotification(Integer.parseInt(notification));
				// }

				if (CommonBll.checkInputs(obj)) {

					int res = Integer.MIN_VALUE;
					try {
						res = dal.update(obj);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					if (res == 0) {
						// TODO success
						response.sendRedirect("agenda.jsp?id=" + obj.getIdAgenda());
					} else {
						// TODO error
					}
				} else {
					request.setAttribute("agendaItem", obj);
					getServletContext().getRequestDispatcher("/agendaItemEdit.jsp").forward(request, response);
				}
			} else {
				// TODO data not found
			}
		} else if (mode.equalsIgnoreCase("delete")) {

			AgendaItemModel query = new AgendaItemModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<AgendaItemModel, AgendaItemModelExt> dal = new GenericDal<AgendaItemModel, AgendaItemModelExt>(new AgendaItemModel());

			int res = Integer.MIN_VALUE;
			try {
				res = dal.delete(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (res == 0) {
				// TODO success
				getServletContext().getRequestDispatcher("/agendaItemList.jsp").forward(request, response);
			} else {
				// TODO fail
			}
		} else if (mode.equalsIgnoreCase("quickInsert")) {
			AgendaItemModel obj = new AgendaItemModel();

			// TODO set name here

			GenericDal<AgendaItemModel, AgendaItemModelExt> dal = new GenericDal<AgendaItemModel, AgendaItemModelExt>(new AgendaItemModel());

			int res = 0;
			try {
				res = dal.insert(obj);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// TODO : return value

		}
	}

}